import './SelectionTemplate.scss'
import { useMount, useRequest } from 'ahooks'
import { useState } from 'react'
import { Button, Input, Select, message } from 'antd'
import { Http } from '../../../Common/Api'

// 批量启用/禁用
export const SelectionTemplateEnabled = ({ selectedRowKeys, selectionType, OptionsEvent, actionRef }) => {
    const { url } = selectionType
    const { runAsync } = useRequest(Http, { manual: true })
    const onSubmit = (uuid, value) => {
        runAsync({ url, data: { uuid, value } }).then(res => {
            actionRef.current.reload()
            OptionsEvent.emit({ type: 'closeSelection' })
        })
    }

    return (
        <div>
            批量启用/禁用 : 已选 {selectedRowKeys.length} 项
            <Button
                type="link"
                onClick={() => {
                    onSubmit(selectedRowKeys, 1)
                }}
            >
                批量启用
            </Button>
            <Button
                type="link"
                danger
                onClick={() => {
                    onSubmit(selectedRowKeys, 0)
                }}
            >
                批量禁用
            </Button>
        </div>
    )
}

// 批量审核
export const SelectionTemplateRecordses = ({ selectedRowKeys, selectionType, OptionsEvent, actionRef }) => {
    const { url } = selectionType
    const { runAsync } = useRequest(Http, { manual: true })
    const onSubmit = (uuid, value) => {
        runAsync({ url, data: { uuid, value } }).then(res => {
            actionRef.current.reload()
            OptionsEvent.emit({ type: 'closeSelection' })
        })
    }

    return (
        <div>
            批量启用/禁用 : 已选 {selectedRowKeys.length} 项
            <Button
                type="link"
                onClick={() => {
                    onSubmit(selectedRowKeys, '通过')
                }}
            >
                批量通过
            </Button>
            <Button
                type="link"
                danger
                onClick={() => {
                    onSubmit(selectedRowKeys, '不通过')
                }}
            >
                批量不通过
            </Button>
        </div>
    )
}

// 批量审核试验室数据
export const SelectLabAuditStatus = ({ selectedRowKeys, selectionType, OptionsEvent, actionRef }) => {
    const { url, value2 } = selectionType
    const [value1, setValue1] = useState('')
    const { runAsync } = useRequest(Http, { manual: true })
    const onSubmit = (uuid, value) => {
        runAsync({ url, data: { uuid, value, value1, value2 } }).then(res => {
            actionRef.current.reload()
            OptionsEvent.emit({ type: 'closeSelection' })
        })
    }
    return (
        <div className="SelectLabAuditStatus">
            试验数据批量审核 : 已选 {selectedRowKeys.length} 项
            <Input placeholder="请输入原因" value={value1} onChange={e => setValue1(e.target.value)} />
            <Button
                type="link"
                onClick={() => {
                    onSubmit(selectedRowKeys, '通过')
                }}
            >
                审核通过
            </Button>
            <Button type="link" danger onClick={() => onSubmit(selectedRowKeys, '不通过')}>
                审核不通过
            </Button>
        </div>
    )
}
//
export const SelectLabHandleType = ({ selectedRowKeys, selectionType, OptionsEvent, actionRef }) => {
    const { url, handle, value2 } = selectionType
    const [messageApi, contextHolder] = message.useMessage()
    const [options, setOptions] = useState([])
    const [value, setValue] = useState('')
    const [value1, setValue1] = useState('')
    const { runAsync } = useRequest(Http, { manual: true })
    const onSubmit = uuid => {
        if (!value) {
            messageApi.info('请选择问题类型!')
            return false
        }
        runAsync({ url, data: { uuid, value, value1, value2 } }).then(res => {
            actionRef.current.reload()
            OptionsEvent.emit({ type: 'closeSelection' })
        })
    }
    useMount(() => {
        setOptions(Object.keys(handle).map(value => ({ label: handle[value], value })))
    })
    return (
        <div className="SelectLabAuditStatus">
            {contextHolder}
            试验数据批量审核 : 已选 {selectedRowKeys.length} 项
            <Select placeholder="请选择问题类型" options={options} onChange={setValue} />
            <Input placeholder="请输入原因" value={value1} onChange={e => setValue1(e.target.value)} />
            <Button type="link" onClick={() => onSubmit(selectedRowKeys)}>
                提交原因
            </Button>
        </div>
    )
}

// 批量删除
export const SelectionDelete = ({ selectedRowKeys, selectionType, OptionsEvent, actionRef }) => {
    const { runAsync } = useRequest(Http, { manual: true })
    const onSubmit = e => {
        runAsync({ url: selectionType.url, data: e }).then(() => {
            actionRef.current.reload()
            OptionsEvent.emit({ type: 'closeSelection' })
        })
    }
    return (
        <div className="SelectionDelete">
            批量删除 : 已选 {selectedRowKeys.length} 项
            <Button
                type="link"
                danger
                onClick={() => {
                    onSubmit(selectedRowKeys)
                }}
            >
                提交删除
            </Button>
        </div>
    )
}
